package app.ui;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import database.local.Business;
import database.local.PicturesUtil;
import database.local.Utils;

public class BusinessListArrayAdapter extends ArrayAdapter<Business> {
	private static List<Business> businessList;
	private Context mContext;
	private LayoutInflater mInflater;
	

	
	
	public BusinessListArrayAdapter (Context context, List<Business> results){
		super (context, R.id.list_tab_layout);
		businessList = results;
		mInflater = LayoutInflater.from(context);
		this.mContext = context;
	}
	
	public int getCount() {
		return businessList.size();
	}

	public Business getItem(int position) {
		return businessList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		
		if (convertView == null){
			convertView = mInflater.inflate(R.layout.list_row_layout, null);
			holder = new ViewHolder();
			
			holder.title = (TextView) convertView.findViewById(R.id.row_title_text);
			holder.rateBar = (RatingBar) convertView.findViewById(R.id.row_rate_ratingbar);
			holder.distance = (TextView) convertView.findViewById(R.id.row_distance_text);
			holder.name = (TextView) convertView.findViewById(R.id.row_business_name_text);
			holder.moneyBar = (RatingBar) convertView.findViewById(R.id.row_moeny_ratingbar);
			holder.image = (ImageView) convertView.findViewById(R.id.row_deal_image);
			holder.address = (TextView) convertView.findViewById(R.id.row_address_text);
			convertView.setTag(holder);
		}
		else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		
		holder.address.setText(businessList.get(position).getFullAddress());				
		holder.rateBar.setRating(businessList.get(position).getDeals().get(0).getRate());
		holder.name.setText("At " + businessList.get(position).getName());
		holder.moneyBar.setRating(businessList.get(position).getDeals().get(0).getCostLevel());
		holder.distance.setText(Utils.getDistanceStr(businessList.get(position).getDistance()));
		holder.image.setImageDrawable(PicturesUtil.getDealPicSmall(businessList.get(position).getDealMaxRate(), mContext));
		holder.title.setText(businessList.get(position).getDealMaxRate().getShortName());
		
		return convertView;
	}
	
	static class ViewHolder {

		TextView title, name, distance, address;
		RatingBar rateBar, moneyBar;
		ImageView image;
		
	}
	

}
